package test;


import divination.yijing.*;
import javax.swing.*;
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

/**
 * @author Ting-Shuo Yo
 *
 */
public class AppletCoinsDivination extends Applet implements ActionListener {
	JPanel panel;
	//private boolean _clickMeMode = true;

	JLabel WELCOME = new JLabel("歡迎使用易經占卜\n");
	JTextArea DESCRIPTION = new JTextArea("\n\n" +
							 "金錢卦始於漢朝，乃取銅錢擲三次為一爻，\n"+
                             "取數多者定陰陽，以三者同否定老少。\n"+
                             "六爻擲畢成二卦，本卦可作近況解，\n"+
                             "而變卦為未來長期之預測。\n\n\n"+
                             "請誠心問事，輕搖手機，然後按下「占卜」\n");
	JTextArea original = new JTextArea("....");
	JTextArea varied = new JTextArea("....");
	JButton button = new JButton("占卜");
	JScrollPane scrollingArea = new JScrollPane(DESCRIPTION);
	JScrollPane sa1 = new JScrollPane(original);
	JScrollPane sa2 = new JScrollPane(varied);
	
	@Override
	public void init(){
		// Layout
		setLayout(new BorderLayout());
		setBackground(Color.white);
		// Action Listener
		button.addActionListener(this);
		// Add items
		//WELCOME.setSize(800, 40);
		//DESCRIPTION.setSize(800,400);
		add(WELCOME,BorderLayout.PAGE_START);
		//add(DESCRIPTION);
		add(sa1,BorderLayout.CENTER);
		//add(sa2);
		add(button,BorderLayout.PAGE_END);
	}
	
	  @Override
	public void start(){
		     System.out.println("Applet starting.");
		  }

		  @Override
		public void stop(){
		     System.out.println("Applet stopping.");
		  }

		  @Override
		public void destroy(){
		     System.out.println("Destroy method called.");
		  }

	
	   public void actionPerformed(ActionEvent event){
	        //Object source = event.getSource();
	    	Random rd = new Random();
	    	String[] results = divinationCoins(rd);
	    	original.setText(results[0]+"\n\n"+results[1]);
	    	//varied.setText(results[1]);
//	        if (_clickMeMode) {
//	        	DESCRIPTION.setText(results[0]);
//	          button.setText("變卦");
//	          _clickMeMode = false;
//	        } else {
//	        	DESCRIPTION.setText(results[1]);
//	          button.setText("重新占卜");
//	          _clickMeMode = true;
//	        }
	   }
	   
    public String[] divinationCoins(Random rd){
    	// Divination
    	DivinationCoins d1 = new DivinationCoins(rd);
    	// Form output strings
    	String[] gua = new String[2];
    	gua[0] = d1.original.getName() + "\n" + 
    			 d1.original.toString()+ "\n" + 
    			 d1.original.getClassicInterpretation()+ "\n" + 
    			 d1.original.getModernInterpretation();
    	gua[1] = d1.varied.getName() + "\n" + 
		 		 d1.varied.toString()+ "\n" + 
		 		 d1.varied.getClassicInterpretation()+ "\n" + 
		 		 d1.varied.getModernInterpretation();
    	// Return results
    	return(gua);
    }
}
